
const _ServiceConfig = {
    ApiService: getApiServiceUrl,
    EntityService: getApiServiceUrl,
    WebService: getWebServiceUrl,
    WebRootPath: getWebRootPath,
    ResourceService: getApiServiceUrl
};

const addServiceConfig = (name, fn) => {
    _ServiceConfig[name] = fn;
};

const EnvConfig = {
    getServiceUrl,
    setEnv,
    addServiceConfig,
    env: null,
    isProd: false,
    appId: 'b58c579f-7288-3169-a259-5a5c03765629',
    token: '4GHORJQRNRhL7I7XYZnctDfwoEGDAYUNyknzU4fBv7z49FgWHIB1yg==',
    prodPath: '',
    tokenKey: 'D145C602-0B96-4684-A8A1-B34C38E3926D',
    userIdKey: '5FA9E4D4-3A13-468E-918D-F9750840F763',
    wxUserKey: 'F29DD7CC-F5FE-4C28-A705-2E7D6F525070'
};

// ctx koa对象，ctx不为空表示是服务器
function setEnv() {
    EnvConfig.env = getWebEnv();
    EnvConfig.isProd = EnvConfig.env === "prd";
}

function getWebEnv() {
    const h = window.location.hostname.toLowerCase();
    return getEnv(h);
}

function getEnv(h) {
    if (h.indexOf("localhost") >= 0 || h.indexOf('192.168.0.104') >= 0) return "local";
    return "prd";
}

function getWebServiceUrl() {
      return EnvConfig.isProd ? EnvConfig.prodPath + "/" : "/";
}

function getWebRootPath() {
     return EnvConfig.isProd ? EnvConfig.prodPath : '';
}

function getApiServiceUrl() {
     return EnvConfig.isProd ? '/api/' : '/api/';
}

function getServiceUrl(serverName) {
    if (!_ServiceConfig[serverName]) serverName = 'ApiService';
    return () => _ServiceConfig[serverName]();
}

EnvConfig.setEnv();

export default EnvConfig;